//
//  EncyclopediaViewController.m
//  ShiWuPai
//
//  Created by app on 16/11/22.
//  Copyright © 2016年 QQ:244987427. All rights reserved.
//

#import "EncyclopediaViewController.h"
#import "SouSuoViewController.h"
#import "WHCategorysCollectionViewCell.h"
#import "CategoryDetailsViewController.h"
#import "WHBaiKeGroupCategory.h"

#import "BaseNetworkRequest.h"
#import "BaseNetworkRequest+GroupCategories.h"

#import "WHHeaderCollectionReusableView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface EncyclopediaViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation EncyclopediaViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    UIImageView *searchView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    searchView.image = [UIImage imageNamed:@"3.png"];
    self.navigationItem.titleView = searchView;
    searchView.userInteractionEnabled = YES;
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    searchBtn.frame = CGRectMake(20, 0, SCREEN_WIDTH - 100  , 35);
    [searchBtn addTarget:self action:@selector(goSearch) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:searchBtn];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.collectionView.backgroundColor=[UIColor whiteColor];
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"WHCategorysCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WHCategorysCollectionViewCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"WHHeaderCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"WHHeaderCollectionReusableView"];
    [self requestData];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    [super viewWillAppear:animated];
    
}

- (void)goSearch{
    SouSuoViewController *souSuoCtr = [SouSuoViewController new];
    [self.navigationController pushViewController:souSuoCtr animated:YES];
    
}
#pragma mark - network request
-(void)requestData{
    [BaseNetworkRequest getGroupCategoriesSuccess:^(id responseObj, NSError *error) {
        WHBaiKeGroupCategory *baiKeGroupCategory=responseObj;
        if (!error) {
            
            //展示数据
            [self.dataArray addObjectsFromArray:baiKeGroupCategory.group];
            [self.collectionView reloadData];
        }
        
        else{
            //show Error
            NSLog(@"error:%@",error);
        }
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    return self.dataArray.count;
    return self.dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
     WHBaiKeGroup *group=self.dataArray[section];
    return group.categories.count;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WHBaiKeGroup *group=self.dataArray[indexPath.section];
    WHBaiKeCategory *category=group.categories[indexPath.row];
    
    WHCategorysCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"WHCategorysCollectionViewCell" forIndexPath:indexPath];
    cell.category=category;
   // cell.backgroundColor=[UIColor orangeColor];
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    WHBaiKeGroup *group=self.dataArray[indexPath.section];
    WHHeaderCollectionReusableView *reusableView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WHHeaderCollectionReusableView" forIndexPath:indexPath];
    if (indexPath.section==0) {
        reusableView.titleLable.text=@"食物分类";
    }
    else if (indexPath.section==1){
        reusableView.titleLable.text=@"热门品牌";
    }
    
    else{
        reusableView.titleLable.text=@"连锁餐饮";
    }
    
    return reusableView;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WHBaiKeGroup *group=self.dataArray[indexPath.section];
    WHBaiKeCategory *category=group.categories[indexPath.row];
   //跳转
    CategoryDetailsViewController *viewController=[[CategoryDetailsViewController alloc] init];
    viewController.category=category;
    viewController.kind=group.kind;
    [self.navigationController pushViewController:viewController animated:YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.view.bounds.size.width/3.0, self.view.bounds.size.width/3.0);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(self.view.bounds.size.width, 45);
}

#pragma mark - setter getter
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}
@end
